import router from '../router'
import { ElMessage } from 'element-plus';

import axios from 'axios'

const ipAddress = '10.2.1.196:8080';
export const host = 'http://' + ipAddress;
const localStorageTokenKey = 'routing-token';
const serverTokenKey = 'access-token';
const request = axios.create({
    baseURL: host,
    timeout: 300000,
    headers: {
        'Content-type': 'application/json',
    }
})

const requestByForm = axios.create({
    baseURL: host,
    timeout: 20000,
    headers: {
        'Content-Type': 'multipart/form-data'
    }
})

var requestByImage = axios.create({
    baseURL: host,
    timeout: 10000,
    responseType: 'blob',
    headers: {

    }
})

function loginError(message) {
    ElMessage({ type: 'error', message: message })
}

export function getImage(url, params) {
    requestByImage.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        let reqUrl = url;
        if (params != undefined && params != null) {
            reqUrl = url + params;
        }
        requestByImage.get(reqUrl).then(res => {
            resolve(res.data)
        }).catch(err => {
            ElMessage({ message: '服务器异常，请稍后重试', type: 'error' });
        })
    })
}

export function get(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        request.get(url, {
            params: params
        }).then(res => {
            if (res != undefined && res.headers[serverTokenKey]) {
                localStorage.setItem(localStorageTokenKey, res.headers[serverTokenKey])
            }
            resolve(res.data);
        }).catch(err => {
            if (err.response != undefined && err.response.status == 401) {
                loginError(err.response.data.data)
                router.push('/login')
            }
            if (err.response != undefined && err.response.status == 503) {
                loginError(err.response.data.path + "服务正在维护，请稍后重试")
            }
            reject(err.data)
        })
    });
}

export function rest(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        request.get(url + '/' + params).then(res => {
            resolve(res.data);
        }).catch(err => {
            if (err.response.status == 401) {
                loginError(err.response.data.data)
                router.push('/login')
            }
            if (err.response.status == 503) {
                loginError(err.response.data.path + "服务正在维护，请稍后重试")
            }
            reject(err.data)
        })
    });
}

export function post(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        request.post(url, JSON.stringify(params))
            .then(res => {
                if (res && res.headers[serverTokenKey]) {
                    localStorage.setItem(localStorageTokenKey, res.headers[serverTokenKey])
                }
                resolve(res.data);
            })
            .catch(err => {
                if (err.response.status == 401) {
                    loginError(err.response.data.data)
                    router.push('/login')
                }
                if (err.response.status == 503) {
                    loginError(err.response.data.path + " 服务正在维护或重启，请稍后重试")
                }
                reject(err)
            })
    });
}
export function del(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        request.delete(url, { data: params }).then(res => {
            if (res.headers[serverTokenKey]) {
                localStorage.setItem(localStorageTokenKey, res.headers[serverTokenKey])
            }
            resolve(res.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function put(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        request.put(url, JSON.stringify(params)).then(res => {
            if (res.headers[serverTokenKey]) {
                localStorage.setItem(localStorageTokenKey, res.headers[serverTokenKey])
            }
            resolve(res.data);
        }).catch(err => {
            reject(err);
        })
    })
}

export function formPost(url, formData) {
    requestByForm.defaults.headers.Authorization = localStorage.getItem(localStorageTokenKey);
    return new Promise((resolve, reject) => {
        requestByForm.post(url, formData).then(res => {
            if (res.headers[serverTokenKey]) {
                localStorage.setItem(localStorageTokenKey, res.headers[serverTokenKey])
            }
            resolve(res)
        }).catch(res => {
            reject(res.data)
        })
    });
}



request.interceptors.response.use((res) => {
    if (!res.data.status && res.data.statusCode == 10003) {
        localStorage.setItem("loginStatus", "no")
    } else {
        return res
    }
})
